﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;
using System.Collections.Generic;
namespace TuyenDungViecLam.User_control_VN
{
    public partial class CVOnline : System.Web.UI.UserControl
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LienKetDuLieu();
            }
        }

        private void LienKetDuLieu()
        {
            LienKet();
            Label lbgoitinhmain = (Label)dlcvonline.Items[0].FindControl("lbgoitinhmain");
            if (lbgoitinhmain.Text == "True")
            {
                lbgoitinhmain.Text = "Nam";
            }
            else
            {
                if (lbgoitinhmain.Text == "False")
                    lbgoitinhmain.Text = "Nữ";
            }
        }

       

        private void LienKet()
        {
            dlcvonline.DataSource = NguoiTimViec_BUS.LayThongTinCVOnline(Session["UserID"].ToString());
            dlcvonline.DataBind();
        }



        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                dlcvonline.EditItemIndex = 0;
                LienKet();
                DropDownList dlnoio = (DropDownList)dlcvonline.Items[0].FindControl("dlnoio");
                dlnoio.DataSource = ChiTietCongViec_BUS.DanhSachNoiLamViec();
                dlnoio.DataTextField = "Ten";
                dlnoio.DataValueField = "MaTinhThanh";
                dlnoio.DataBind();
                Label lbgioitinh = (Label)dlcvonline.Items[0].FindControl("lbgioitinh");
                RadioButton rbnam = (RadioButton)dlcvonline.Items[0].FindControl("rbnam");
                RadioButton rbnu = (RadioButton)dlcvonline.Items[0].FindControl("rbnu");

                if (lbgioitinh.Text == "True")
                {
                    rbnam.Checked = true;
                }
                if (lbgioitinh.Text == "False")
                {
                    rbnu.Checked = true;
                }
                Label lbnoio = (Label)dlcvonline.Items[0].FindControl("lbnoio");
                for (int i = 0; i < dlnoio.Items.Count; i++)
                {
                    if (dlnoio.Items[i].Text == lbnoio.Text)
                    {
                        dlnoio.SelectedIndex = i;
                        break;
                    }
                }
                DropDownList dltrinhdo = (DropDownList)dlcvonline.Items[0].FindControl("dltrinhdo");
                Label lbtrinhdo = (Label)dlcvonline.Items[0].FindControl("lbtrinhdo");
                for (int i = 0; i < dltrinhdo.Items.Count; i++)
                {
                    if (dltrinhdo.Items[i].Text == lbtrinhdo.Text)
                    {
                        dltrinhdo.SelectedIndex = i;
                        break;
                    }
                }


            }
            if (e.CommandName == "Update")
            {

                TextBox tbhoten = (TextBox)dlcvonline.Items[0].FindControl("tbhoten");
                TextBox tbemail = (TextBox)dlcvonline.Items[0].FindControl("tbemail");
                TextBox tbdienthoai = (TextBox)dlcvonline.Items[0].FindControl("tbdienthoai");
                TextBox tbdidong = (TextBox)dlcvonline.Items[0].FindControl("tbdidong");
                TextBox tbdiachi = (TextBox)dlcvonline.Items[0].FindControl("tbdiachi");

                TextBox tbnamkinhnghiem = (TextBox)dlcvonline.Items[0].FindControl("tbnamkinhnghiem");
                TextBox tbluong = (TextBox)dlcvonline.Items[0].FindControl("tbluong");
                TextBox tbvitri = (TextBox)dlcvonline.Items[0].FindControl("tbvitri");
                TextBox tbgioithieu = (TextBox)dlcvonline.Items[0].FindControl("tbgioithieu");
                RadioButton rbnam = (RadioButton)dlcvonline.Items[0].FindControl("rbnam");
                RadioButton rbnu = (RadioButton)dlcvonline.Items[0].FindControl("rbnu");
                DropDownList dltrinhdo = (DropDownList)dlcvonline.Items[0].FindControl("dltrinhdo");
                DropDownList dlnoio = (DropDownList)dlcvonline.Items[0].FindControl("dlnoio");
                CVONLINE cv = new CVONLINE();
                cv.HoTen = tbhoten.Text;
                cv.Email = tbemail.Text;
                cv.DienThoai = tbdienthoai.Text;
                cv.DiDong = tbdidong.Text;
                cv.DiaChi = tbdiachi.Text;
                cv.NamKinhNghiem = tbnamkinhnghiem.Text;
                if (tbluong.Text != "")
                    cv.MucLuong = Convert.ToDecimal(tbluong.Text);
                cv.ViTriMongMuon = tbvitri.Text;
                cv.GioiThieu = tbgioithieu.Text;
                if (rbnam.Checked == true)
                {
                    cv.GioiTinh = true;
                }
                if (rbnu.Checked == true)
                {
                    cv.GioiTinh = false;
                }
                cv.MaTinhThanh = int.Parse(dlnoio.SelectedItem.Value);
                cv.TrinhDo = dltrinhdo.SelectedItem.Text;
                NguoiTimViec_BUS.CapNhatCVOnline(cv, Session["UserID"].ToString());
                dlcvonline.EditItemIndex = -1;
                LienKetDuLieu();
            }
            if (e.CommandName == "Cancel")
            {
                dlcvonline.EditItemIndex = -1;
                LienKetDuLieu();
            }
        }
    }
}